package com.ayitubie.refuelweb.config.websocket;

import com.ayitubie.refuelweb.config.thread.MyThreadPool;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;

import java.util.Map;
import java.util.concurrent.CompletableFuture;

@Service
//@EnableAsync
@EnableScheduling
@Slf4j
public class SendOnlineCount {
    @Autowired
    MyThreadPool myThreadPool;

    @Scheduled(cron = "0/5 * * * * ?")
//    @Async
    public void sendCountNum() {
        CompletableFuture.runAsync(() -> {
            for (Map.Entry entry : WebSocketServer.webSocketMap.entrySet()) {
                WebSocketServer.sendInfo(Integer.toString(WebSocketServer.getOnlineCount()), entry.getKey().toString());
            }
        }, myThreadPool.threadPoolExecutor());

    }
}
